package 接口.Clonable接口和深拷贝;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-06-19
 * Time: 14:54
 */
class Money1 implements Cloneable {
    public double m = 99.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Person1 implements Cloneable {
    public Money1 money = new Money1();

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //return super.clone();
        Person1 tmp = (Person1) super.clone();
        tmp.money = (Money1) this.money.clone();
        return tmp;
    }
}
public class TestDemo1 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person1 person1 = new Person1();
        Person1 person2 = (Person1)person1.clone();

        System.out.println("通过person2修改前的结果");
        System.out.println(person1.money.m);
        System.out.println(person2.money.m);

        person2.money.m = 100.1;

        System.out.println("通过person2修改后的结果");
        System.out.println(person1.money.m);
        System.out.println(person2.money.m);
    }
}

/*
执行结果：
通过person2修改前的结果
99.9
99.9
通过person2修改后的结果
99.9
100.1
 */